<template>
  <div class="carriers-management">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span class="title">承运商管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增承运商
          </el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="承运商名称">
          <el-input v-model="searchForm.name" placeholder="请输入承运商名称" clearable />
        </el-form-item>
        <el-form-item label="承运商类型">
          <el-select v-model="searchForm.type" placeholder="请选择类型" clearable>
            <el-option label="快递公司" value="express" />
            <el-option label="物流公司" value="logistics" />
            <el-option label="专线运输" value="dedicated" />
            <el-option label="货运代理" value="freight" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="启用" value="active" />
            <el-option label="停用" value="inactive" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table :data="tableData" v-loading="loading" stripe>
        <el-table-column prop="code" label="承运商编码" width="150" />
        <el-table-column prop="name" label="承运商名称" width="180" />
        <el-table-column prop="type" label="类型" width="120">
          <template #default="{ row }">
            <el-tag>{{ getTypeText(row.type) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="contact" label="联系人" width="120" />
        <el-table-column prop="phone" label="联系电话" width="140" />
        <el-table-column prop="email" label="邮箱" width="180" />
        <el-table-column prop="serviceArea" label="服务区域" width="150" />
        <el-table-column prop="rating" label="评分" width="120">
          <template #default="{ row }">
            <el-rate v-model="row.rating" disabled show-score />
          </template>
        </el-table-column>
        <el-table-column prop="cooperationYears" label="合作年限" width="120" align="center">
          <template #default="{ row }">
            {{ row.cooperationYears }} 年
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-switch
              v-model="row.status"
              active-value="active"
              inactive-value="inactive"
              @change="handleStatusChange(row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" size="small" @click="handleView(row)">
              <el-icon><View /></el-icon>
              详情
            </el-button>
            <el-button link type="primary" size="small" @click="handleEdit(row)">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button link type="primary" size="small" @click="handleSettle(row)">
              <el-icon><Wallet /></el-icon>
              结算
            </el-button>
            <el-button link type="danger" size="small" @click="handleDelete(row)">
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="800px"
      @close="handleDialogClose"
    >
      <el-form :model="formData" :rules="rules" ref="formRef" label-width="120px">
        <el-tabs v-model="activeTab">
          <el-tab-pane label="基本信息" name="basic">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="承运商编码" prop="code">
                  <el-input v-model="formData.code" placeholder="请输入承运商编码" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="承运商名称" prop="name">
                  <el-input v-model="formData.name" placeholder="请输入承运商名称" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="承运商类型" prop="type">
                  <el-select v-model="formData.type" placeholder="请选择类型">
                    <el-option label="快递公司" value="express" />
                    <el-option label="物流公司" value="logistics" />
                    <el-option label="专线运输" value="dedicated" />
                    <el-option label="货运代理" value="freight" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="服务区域" prop="serviceArea">
                  <el-input v-model="formData.serviceArea" placeholder="请输入服务区域" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="联系人" prop="contact">
                  <el-input v-model="formData.contact" placeholder="请输入联系人" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系电话" prop="phone">
                  <el-input v-model="formData.phone" placeholder="请输入联系电话" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="formData.email" placeholder="请输入邮箱" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="合作年限" prop="cooperationYears">
                  <el-input-number v-model="formData.cooperationYears" :min="0" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="详细地址" prop="address">
                  <el-input v-model="formData.address" placeholder="请输入详细地址" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="资质信息" name="qualification">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="营业执照号">
                  <el-input v-model="formData.licenseNo" placeholder="请输入营业执照号" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="运输许可证">
                  <el-input v-model="formData.transportPermit" placeholder="请输入运输许可证号" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="法人代表">
                  <el-input v-model="formData.legalPerson" placeholder="请输入法人代表" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="注册资本">
                  <el-input-number v-model="formData.registeredCapital" :min="0" :precision="2" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="结算信息" name="settlement">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="结算方式">
                  <el-select v-model="formData.settlementType" placeholder="请选择结算方式">
                    <el-option label="月结" value="monthly" />
                    <el-option label="季结" value="quarterly" />
                    <el-option label="现结" value="cash" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="账期(天)">
                  <el-input-number v-model="formData.paymentDays" :min="0" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="开户银行">
                  <el-input v-model="formData.bankName" placeholder="请输入开户银行" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="银行账号">
                  <el-input v-model="formData.bankAccount" placeholder="请输入银行账号" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="其他信息" name="other">
            <el-form-item label="备注">
              <el-input
                v-model="formData.remark"
                type="textarea"
                :rows="4"
                placeholder="请输入备注信息"
              />
            </el-form-item>
          </el-tab-pane>
        </el-tabs>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Plus, Search, Refresh, Edit, Delete, View, Wallet
} from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  name: '',
  type: '',
  status: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
})

// 对话框
const dialogVisible = ref(false)
const dialogTitle = ref('新增承运商')
const formRef = ref(null)
const activeTab = ref('basic')

// 表单数据
const formData = reactive({
  code: '',
  name: '',
  type: '',
  serviceArea: '',
  contact: '',
  phone: '',
  email: '',
  cooperationYears: 0,
  address: '',
  licenseNo: '',
  transportPermit: '',
  legalPerson: '',
  registeredCapital: 0,
  settlementType: '',
  paymentDays: 0,
  bankName: '',
  bankAccount: '',
  remark: ''
})

// 表单验证规则
const rules = {
  code: [{ required: true, message: '请输入承运商编码', trigger: 'blur' }],
  name: [{ required: true, message: '请输入承运商名称', trigger: 'blur' }],
  type: [{ required: true, message: '请选择承运商类型', trigger: 'change' }],
  contact: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }]
}

// 获取类型文本
const getTypeText = (type) => {
  const textMap = {
    express: '快递公司',
    logistics: '物流公司',
    dedicated: '专线运输',
    freight: '货运代理'
  }
  return textMap[type] || type
}

// 加载数据
const loadData = () => {
  loading.value = true
  
  setTimeout(() => {
    tableData.value = [
      {
        id: 1,
        code: 'SF001',
        name: '顺丰速运',
        type: 'express',
        contact: '张经理',
        phone: '400-811-1111',
        email: 'service@sf-express.com',
        serviceArea: '全国',
        rating: 4.8,
        cooperationYears: 5,
        status: 'active'
      },
      {
        id: 2,
        code: 'YT001',
        name: '圆通速递',
        type: 'express',
        contact: '李经理',
        phone: '95554',
        email: 'service@yto.net.cn',
        serviceArea: '全国',
        rating: 4.5,
        cooperationYears: 3,
        status: 'active'
      },
      {
        id: 3,
        code: 'ZT001',
        name: '中通快递',
        type: 'express',
        contact: '王经理',
        phone: '95311',
        email: 'service@zto.com',
        serviceArea: '全国',
        rating: 4.6,
        cooperationYears: 4,
        status: 'active'
      }
    ]
    pagination.total = 3
    loading.value = false
  }, 500)
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  loadData()
}

// 重置
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

// 新增
const handleAdd = () => {
  dialogTitle.value = '新增承运商'
  activeTab.value = 'basic'
  dialogVisible.value = true
}

// 查看详情
const handleView = (row) => {
  ElMessage.info(`查看承运商: ${row.name}`)
}

// 编辑
const handleEdit = (row) => {
  dialogTitle.value = '编辑承运商'
  Object.assign(formData, row)
  activeTab.value = 'basic'
  dialogVisible.value = true
}

// 结算
const handleSettle = (row) => {
  ElMessage.info(`进入结算页面: ${row.name}`)
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该承运商吗？', '提示', {
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    loadData()
  })
}

// 状态改变
const handleStatusChange = (row) => {
  const status = row.status === 'active' ? '启用' : '停用'
  ElMessage.success(`已${status}承运商: ${row.name}`)
}

// 提交表单
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success(dialogTitle.value === '新增承运商' ? '创建成功' : '更新成功')
      dialogVisible.value = false
      loadData()
    }
  })
}

// 对话框关闭
const handleDialogClose = () => {
  formRef.value?.resetFields()
}

// 分页改变
const handleSizeChange = () => {
  loadData()
}

const handleCurrentChange = () => {
  loadData()
}

onMounted(() => {
  loadData()
})
</script>

<style scoped lang="scss">
.carriers-management {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      font-size: 16px;
      font-weight: 500;
    }
  }

  .search-form {
    margin-bottom: 20px;
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>

